package cn.org.atool.generator;

import cn.org.atool.fluent.mybatis.metadata.DbType;
import cn.org.atool.generator.annotation.Relation;
import cn.org.atool.generator.annotation.RelationType;
import cn.org.atool.generator.annotation.Table;
import cn.org.atool.generator.annotation.Tables;
import org.apache.commons.dbcp2.BasicDataSource;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.test4j.module.database.proxy.DataSourceCreator;

import javax.sql.DataSource;
import java.util.List;

/**
 * DataMapGeneratorTest
 *
 * @author darui.wu 2020/5/7 11:47 上午
 */
public class GeneratorMain {
    private final static String url = "jdbc:mysql://localhost:3306/fluent_mybatis?useUnicode=true&characterEncoding=utf8";

    public static DataSource newDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl(url);
        dataSource.setUsername("root");
        dataSource.setPassword("password");
        return dataSource;
    }

    @Test
    @Disabled
    public void generate() {
        DataSourceCreator.create("dataSource");
        FileGenerator.setColumn("student", "address", "address")
            .setJavaType(List.class, String.class);
        FileGenerator.build(newDataSource(), Empty1.class, Empty2.class);
    }

    @Tables(
        dbType = DbType.MYSQL,
        schema = "fluent_mybatis",
        srcDir = "src/main/java",
        testDir = "src/test/java",
        daoDir = "target/dao",
        basePack = "cn.org.atool.generator.demo",
        tables = {
            @Table(value = "home_address:MyAddressPoJo"),
            @Table(value = "student", tablePrefix = "t_",
                gmtCreated = "gmt_created", gmtModified = "gmt_modified")
        },
        relations = {
            @Relation(source = "student", target = "home_address", type = RelationType.TwoWay_N_1,
                where = "home_address_id=id && env=env && is_deleted=is_deleted"),
            @Relation(method = "findAnother", source = "student", target = "home_address", type = RelationType.OneWay_0_1, cache = false)
        },
        entitySuffix = "PoJo", logicDeleted = "is_deleted", selectKeyBefore = 0
    )
    static class Empty1 {
    }

    @Tables(
        url = url, username = "root", password = "password",
        schema = "fluent_mybatis",
        srcDir = "src/main/java",
        testDir = "src/test/java",
        daoDir = "target/dao",
        basePack = "cn.org.atool.generator.demo",
        tables = @Table(value = {"no_auto_id", "no_primary"}, mapperPrefix = "new"),
        version = "lock_version",
        isLombok = false, isSetterChain = false, isRichEntity = false
    )
    static class Empty2 {
    }
}